package data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;

import util.DateUtil;


public class LoadHistoryData {
	public HistoryData loadData() {
		HistoryData hd = null;
		try {
			FileInputStream fis;
			Date date = DateUtil.getLastDealDate(new Date());
			String fileName = DateUtil.format(date);
			File file = new File("historydata"+File.separator+fileName+".sto");
			while(!file.exists()){
				date = DateUtil.getLastDealDate(date);
				file = new File("historydata"+File.separator+DateUtil.format(date)+".sto");
				if(date.getYear()<113){
					SaveHistoryData shd  = new SaveHistoryData();
					shd.saveHistoryData();
					file = new File("historydata"+File.separator+"last.sto");
				}
			}
			fis = new FileInputStream(file);
			ObjectInputStream oin = new ObjectInputStream(fis);          
			hd = (HistoryData) oin.readObject();          
			oin.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return hd;
	}
	
	public HistoryData loadData(String date) {
		HistoryData hd = null;
		try {
			FileInputStream fis;
			fis = new FileInputStream("historydata"+File.separator+date);
			ObjectInputStream oin = new ObjectInputStream(fis);          
			hd = (HistoryData) oin.readObject();          
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return hd;
	}
}
